package com.xueyi.workflow.service.impl;

import com.xueyi.workflow.domain.dto.WfProcessApprovalDto;
import com.xueyi.workflow.domain.entity.WfProcessApproval;
import com.xueyi.workflow.domain.query.WfProcessApprovalQuery;
import com.xueyi.workflow.manager.WfProcessApprovalManager;
import com.xueyi.workflow.service.IWfProcessApprovalService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 流程审批Service业务层处理
 *
 * @author xueyi
 * @date 2024-07-03
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WfProcessApprovalServiceImpl implements IWfProcessApprovalService {

    private final WfProcessApprovalManager wfProcessApprovalManager;

    /**
     * 查询审批历史
     */
    @Override
    public List<WfProcessApprovalDto> getApprovalHistory(String processInstanceId) {
        log.info("查询审批历史，流程实例ID: {}", processInstanceId);

        try {
            // 构建查询条件
            WfProcessApprovalQuery query = new WfProcessApprovalQuery();
            query.setProcessInstanceId(processInstanceId);
            
            log.info("构建查询条件: {}", query);

            // 查询数据库中的审批记录
            List<WfProcessApprovalDto> approvalHistory = wfProcessApprovalManager.selectList(query);

            log.info("查询到审批历史记录数量: {}", approvalHistory.size());
            
            // 打印每条记录的详细信息
            for (int i = 0; i < approvalHistory.size(); i++) {
                WfProcessApprovalDto record = approvalHistory.get(i);
                log.info("审批记录[{}]: processInstanceId={}, taskId={}, nodeName={}, approver={}, approvalResult={}, approvalTime={}", 
                    i + 1, record.getProcessInstanceId(), record.getTaskId(), record.getNodeName(), 
                    record.getApprover(), record.getApprovalResult(), record.getApprovalTime());
            }
            
            return approvalHistory;

        } catch (Exception e) {
            log.error("查询审批历史失败，流程实例ID: {}", processInstanceId, e);
            // 返回空列表而不是假数据
            return List.of();
        }
    }

    /**
     * 添加审批记录
     */
    @Override
    public boolean addApprovalRecord(WfProcessApprovalQuery query) {
        log.info("添加审批记录，流程实例ID: {}", query.getProcessInstanceId());

        try {
            // 转换查询对象为DTO
            WfProcessApprovalDto dto = new WfProcessApprovalDto();
            dto.setProcessInstanceId(query.getProcessInstanceId());
            dto.setProcessId(query.getProcessId());
            dto.setProcessKey(query.getProcessKey());
            dto.setProcessName(query.getProcessName());
            dto.setTaskId(query.getTaskId());
            dto.setNodeId(query.getNodeId());
            dto.setNodeName(query.getNodeName());
            dto.setApprovalType(query.getApprovalType());
            dto.setApprovalResult(query.getApprovalResult());
            dto.setApprovalOpinion(query.getApprovalOpinion());
            dto.setApprover(query.getApprover());
            dto.setApproverId(query.getApproverId());
            dto.setApprovalTime(query.getApprovalTime());
            dto.setNextAssignee(query.getNextAssignee());
            dto.setCcUsers(query.getCcUsers());
            dto.setNotifyMethods(query.getNotifyMethods());

            // 插入数据库
            int result = wfProcessApprovalManager.insert(dto);

            log.info("审批记录添加成功，影响行数: {}", result);
            return result > 0;

        } catch (Exception e) {
            log.error("添加审批记录失败，流程实例ID: {}", query.getProcessInstanceId(), e);
            return false;
        }
    }
}
